﻿using System;
using System.Windows.Browser;


namespace SilverlightAnalytics
{
    
    /// <summary>
    /// Represents anything that interacts with Google Analytics
    /// </summary>
    public abstract class PageTracker
    {

        private ScriptObject so;

        /// <summary>
        /// Gets a <see cref="ScriptObject"/> reference to the <see cref="HtmlDocument"/>'s Google Page Tracker instance
        /// </summary>
        /// <remarks>
        /// For any of this to work, you need to drop in the new version of the Google Analytics tracker (ga.js, not urchin.js)!s
        /// <note type="caution">This property will return <b>null</b> if the tracking code has not been installed!</note>
        /// </remarks>
        /// <example>
        /// The following example shows how you might use this property for something interesting...
        /// <code lang="JavaScript" title="The tracking code on your page (JavaScript)" filter="false">
        /// <![CDATA[
        ///         var pageTracker = _gat._getTracker("your-code");  // <---- pageTracker is what we use!!!
        /// ]]>
        /// </code>
        /// Here is some code that calls the <a href="http://code.google.com/apis/analytics/docs/gaJSApi.html#_gat.GA_Tracker_._trackPageview">_trackPageview()</a> method:
        /// <code lang="cs">
        /// // a bunch of categories
        /// public enum MyCagetories {
        ///     Video,
        ///     Music
        /// }
        /// 
        /// // a bunch of actions
        /// public enum MyActions {
        ///     Play,
        ///     Pause
        /// }
        /// 
        /// public class SomeClass {
        ///     private EventTracker&lt;MyCategories, MyActions&gt; videoTracker;
        ///
        ///     public TestControl() {
        ///         // create instance of event tracker for Videos
        ///         videoTracker = new EventTracker&lt;MyCategories, MyActions&gt;(MyCategories.Video);
        /// 
        ///     }
        ///     public void TrackAPage() 
        ///     {
        ///         // track a page view
        ///         videoTracker.Tracker.Invoke("_trackPageview", "/home/landingPage");
        ///     }
        /// }
        /// </code>
        /// </example>
        /// <value>The value returned is managed code reference to Google's javascript pageTracker API.  More information about this API can be found <a href="http://code.google.com/apis/analytics/docs/gaJSApi.html" title="Google Analytics Tracking API">here</a>.
        /// </value>
        public ScriptObject Tracker
        {
            get {
                if (so == null)
                {
                    so = (ScriptObject)HtmlPage.Window.GetProperty("pageTracker");
                }
                return so; 
            }
            
        }

   
    }
}
